package zb.WX.service.account;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import zb.WX.bean.account.ShortUrl;
import zb.WX.util.AccessTokenSaveUtil;
import zb.WX.util.GlobalVar;
import zb.WX.util.HttpsUtil;


/**
 * 长连接转为短链接
 *
 * @author Administrator
 */
public class ShortUrlService {
    /*
     * 长链接转短链接：
     *
     */
    public String getShortUrl(String long_url) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.GET_SHORT_URL.replace("ACCESS_TOKEN", access_token);

        ShortUrl shortUrl = new ShortUrl();
        shortUrl.setAccess_token(access_token);
        shortUrl.setAction(GlobalVar.LONG_TO_SHORT);
        shortUrl.setLong_url(long_url);

        //注意：因为gson会把某些字符，如=，替换为unicode，所以采用下面的方式创建gson
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String jsonStr = gson.toJson(shortUrl);
        System.out.println("转化地址：" + jsonStr);

        String result = HttpsUtil.getHttpsToJson(urlStr, jsonStr);
        System.out.println("转化地址结果：" + result);

        return result.replace("\\", "");
    }

    public static void main(String[] args) {
        String shortUrl = new ShortUrlService().getShortUrl("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFO7zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAySm9sQ01xdVJjNG0xYVl2VU5yMVUAAgQ8ZS9bAwSAOgkA");
        System.out.println(shortUrl);
    }
}
